/*
 *  Copyright (C) 2010 Matthias Buch-Kromann <mbk.isv@cbs.dk>
 * 
 *  This file is part of the MatrixParser package.
 *  
 *  The MatrixParser program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 * 
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.osdtsystem.matrixparser.features;

import java.util.AbstractCollection;
import java.util.Iterator;
import java.util.Map.Entry;

/**
 *
 * @author Matthias Buch-Kromann <mbk.isv@cbs.dk>
 */
public class SparseDuplicateDebugFeatureVector extends AbstractFeatureVector {
    SparseBinaryFeatureVector fvSet = new SparseBinaryFeatureVector();
    SparseFeatureVector fvMap = new SparseFeatureVector(true);

    @Override
    public int size() {
        int sizeSet = fvSet.size();
        int sizeMap = fvMap.size();
        if (sizeSet != sizeMap)
            throw new IllegalStateException("set has size " + sizeSet + ", map has size " + sizeMap);
        return sizeSet;
    }

    public double getValue(Feature f) {
        double valueSet = fvSet.getValue(f);
        double valueMap = fvMap.getValue(f);
        if (Double.compare(valueSet, valueMap) != 0)
            throw new IllegalStateException("set has value " + valueSet + ", map has value " + valueMap);
        return valueSet;
    }

    public void setValue(Feature f, double v) {
        fvSet.setValue(f, v);
        fvMap.setValue(f, v);
        double valueSet = fvSet.getValue(f);
        double valueMap = fvMap.getValue(f);
        if (Double.compare(valueSet, valueMap) != 0)
            throw new IllegalStateException("set has value " + valueSet + ", map has value " + valueMap);
    }

    public void addValue(Feature f, double v) {
        fvSet.addValue(f, v);
        fvMap.addValue(f, v);
        double valueSet = fvSet.getValue(f);
        double valueMap = fvMap.getValue(f);
        if (Double.compare(valueSet, valueMap) != 0)
            throw new IllegalStateException("set has value " + valueSet + ", map has value " + valueMap);
    }

    public void ignoreFeatures(FeatureVector fv) {
        fvSet.ignoreFeatures(fv);
        fvMap.ignoreFeatures(fv);
        if (! ((FeatureVector)fvSet).equals((FeatureVector) fvMap))
            throw new IllegalStateException("the two feature vectors have become different after ignoreFeatures");
    }

    public void add(FeatureVector fv) {
        fvSet.add(fv);
        fvMap.add(fv);
        if (! ((FeatureVector)fvSet).equals((FeatureVector) fvMap))
            throw new IllegalStateException("the two feature vectors have become different after ignoreFeatures");
    }

    public int checksum() {
        return fvSet.checksum() + fvMap.checksum();
    }

    public String checksum64() {
        return fvSet.checksum64() + "/" + fvMap.checksum64();
    }

    @Override
    public Iterator<Entry<Feature, Double>> iterator() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void addValue(FeatureHandler fh, CharSequence chars, double v) {
        addValue(fh.getFeature(chars.toString()), v);
    }

    class SparseDuplicateDebugFeatureVectorIterator implements Iterator<Entry<Feature,Double>> {
        Iterator<Entry<Feature,Double>> iteratorSet = fvSet.iterator();
        Iterator<Entry<Feature,Double>> iteratorMap = fvMap.iterator();

        public boolean hasNext() {
            boolean hasNextSet = iteratorSet.hasNext();
            boolean hasNextMap = iteratorMap.hasNext();
            if (hasNextSet != hasNextMap)
                throw new IllegalStateException("hasNextSet=" + hasNextSet + ", hasNextMap=" + hasNextMap);
            return hasNextSet;
        }

        public Entry<Feature, Double> next() {
            Entry<Feature,Double> nextSet = iteratorSet.next();
            Entry<Feature,Double> nextMap = iteratorSet.next();
            if (! nextSet.getKey().equals(nextMap.getKey()))
                throw new IllegalStateException("key mismatch: " + nextSet.getKey() + " != " + nextMap.getKey());
            return nextSet;
        }

        public void remove() {
            throw new UnsupportedOperationException("Not supported yet.");
        }
    }
}
